/**
 * 文件：ModifyScorePanel.java
 * 说明：用于修改课程
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ModifyScorePanel extends JPanel implements ActionListener,MouseListener,ItemListener
{
	private static final long serialVersionUID = -5537079055059926383L;
	private JLabel sNo = new JLabel("学        号：");                               //静态标签
	private JLabel sName = new JLabel("姓        名：");
	private JLabel cID = new JLabel("课程编号：");         
	private JLabel cLesson = new JLabel("课  程  名：");      
	private JLabel scScore = new JLabel("成        绩：");          
	
	private JTextField sno = new JTextField("请输入学号",10);                 //文本输入
	private JTextField sname = new JTextField("",10); 
	private JComboBox cname = new JComboBox();   
	private JTextField cno = new JTextField("",10);  
    private JTextField grade = new JTextField("",10);
    
    
    private JButton query = new JButton("查询");                                  //声明按钮
    private JButton ok = new JButton("确定");
    private JButton modify = new JButton("修改");
    	
    private JPanel JPcomon1 = new JPanel();                                        //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    private String str1[] = new String[20];                                             //存储选择列表
    private String str2[] = new String[5];
    private String str3[] = new String[3];    
    
    public ModifyScorePanel()                                                               //构造函数
    {
    	sNo.setHorizontalAlignment(SwingConstants.CENTER);
    	sName.setHorizontalAlignment(SwingConstants.CENTER);
    	cID.setHorizontalAlignment(SwingConstants.CENTER);
    	cLesson.setHorizontalAlignment(SwingConstants.CENTER);
    	scScore.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(sNo);
    	JPcomon1.add(sno);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(sName);
    	JPcomon2.add(sname);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(cLesson);
    	JPcomon3.add(cname);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(cID);
    	JPcomon4.add(cno);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(scScore);
    	JPcomon5.add(grade);

    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(modify);
    	JPbutton.add(ok);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon3);
    	this.add(JPcomon2);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPbutton);
    	
    	query.addActionListener(this);                                          //事件监听
    	ok.addActionListener(this);                                              
    	modify.addActionListener(this);
    	sno.addMouseListener(this);    	
  }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			query.setEnabled(false);
			sno.setEnabled(false);
			sname.setEnabled(false);
			cno.setEnabled(false);
			grade.setEnabled(false);
			cname.setEnabled(false);
			cname.addItemListener(this);
		}
		else if(e.getActionCommand().equals("修改"))
		{				
			grade.setEnabled(true);
			cname.setEnabled(true);
			Modify();
		}
		else if(e.getActionCommand().equals("确定"))
		{
			sno.setEnabled(true);
			Ok();
			query.setEnabled(true);
			cname.removeAllItems();
		}
	}		
	private void QueryDataBase()
	{		
		ResultSet rs = null;
		try
		{
			BaseSQL score = null;
			int i = -1;
			score = new BaseSQL();
			String sql = "select * from sc where sno = '"+Integer.parseInt(sno.getText())+"'" ; 			
		    rs = score.executeSelect(sql);		    
		    for(i = 0;rs.next();i++)
    		{
		    	cname.addItem(rs.getString("cname"));	
    			str1[i]=rs.getString("cname");
    			sname.setText(rs.getString("sname"));	
    			cno.setText(rs.getString("cno"));
    			str2[i]=rs.getString("cno");
    			grade.setText(rs.getString("grade"));
    			str3[i]=rs.getString("grade");
    		} 	
		    cname.setSelectedIndex(i-1);
		    rs.close();		    
		    if(i<=0)
		    {
		    	JOptionPane.showMessageDialog(null,"不存在学号为："+sno.getText()+" 的成绩记录","成绩错误",JOptionPane.INFORMATION_MESSAGE);
		    }
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this,evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	private void Modify()
	{
		sname.setEditable(false);
		cname.setEditable(false);
		cno.setEditable(false);
		grade.setEditable(true);  
	}
	private void Ok()
	{
		try
		{
			BaseSQL score = null;
			score = new BaseSQL();
			String sql = "update sc" +
					" set grade='"+Integer.parseInt(grade.getText())+"'  where cno = '"+Integer.parseInt(cno.getText())+"' AND sno='"+Integer.parseInt(sno.getText())+"'";
			score.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "修改成功", "修改课程信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
	public void itemStateChanged(ItemEvent e) 
	{		
		if(e.getStateChange()==ItemEvent.SELECTED)
		{
			int i = cname.getItemCount(); 
			if(e.getSource() == cname ) 
			{
				for(;i>=0;i--)
				{
					if(e.getItem().equals(str1[i]))
					{
						cno.setText(str2[i]);
						grade.setText(str3[i]);
					}
			    }
			}
		}		
	}	
    public void mouseClicked(MouseEvent e) { }                                           //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(sno.getText().equals("请输入学号"))
    	{    		
    		sno.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

